#include "basic.h"
#include "string.h"






void initList(Seqlist & l){
    for(int i=0;i<MAXSIZE;i++){
        l.data[i].number=0;
        strcpy(l.data[i].name, "aaa");
    }
    l.length=0;
}

/* bool destroyList(Seqlist & l){
    如果是局部函数栈空间中的数组，有程序自动回收


} */

bool insertList(Seqlist & l,int n,int number,string name){
    //插入位置检查
    if(n<1||n>l.length+1){
        cout <<"插入位置不合法" <<endl;
        return false;
    }
    //表满检查
    if(l.length>=MAXSIZE){
        cout <<"表已满" <<endl;
        return false;
    }
    /* //从最后开始往后移一个位置
    for(int i=l.length-1;i>=n-1;i--){
        l.data[i+1]=l.data[i];
    }
    l.data[n-1]=e;
    l.length++;
    return true; */
    for(int i=l.length;i>=n;i--){
        l.data[i].name=l.data[i-1].name;
        l.data[i].number=l.data[i-1].number;

    }
    l.data[n-1].name=name;
    l.data[n-1].number=number;
    l.length++;
    return true;
}

bool deleteListByOrder(Seqlist & l,int n,int &number,string & name){
    if(n<1||n>l.length){
        cout <<"删除位置不合法" <<endl;
        return false;

    }

    number=l.data[n-1].number;
    name=l.data[n-1].name;
    for(int i=n;i<l.length;i++){
        l.data[i-1].name=l.data[i].name;
        l.data[i-1].number=l.data[i].number;
    }
    l.length--;
    return true;

}

void findElementByOrder(Seqlist  l,int n,int &number,string & name){
    if(n<1||n>l.length){
        cout <<"查找位置不合法" <<endl;
        return;

    }
    number=l.data[n-1].number;
    name=l.data[n-1].name;

}

int findElementByValue(Seqlist  l,string name){
    for(int i=0;i<l.length;i++){
        if(l.data[i].name==name){
            return i+1;
        }
    }
    return -1;


}

int findStructByValue(Seqlist  l,Student student){
    for(int i=0;i<l.length;i++){
        if(isSameStudent(l.data[i],student)){
            return i+1;
        }
    }
    return -1;



}

void printList(Seqlist  l){
    for (int i=0;i<l.length;i++){
        cout <<"第" << i+1 <<"个学生序号是 " << l.data[i].number <<endl;
        cout <<"第" << i+1 <<"个学生名字是 " << l.data[i].name <<endl;
    }
}

int lengthOfList(Seqlist l){
    return l.length ;
}

bool isEmpty(Seqlist l){
    if(l.length!=0){
        return false;
    }
    return true;
}

bool isSameStudent(Student a,Student b){
    if(a.name==b.name&&a.number==b.number){
        return true;
    }
    return false;
}


//----------------------------------------------------------------
void initDynamicList(Dynamiclist & l){
    l.data=(Student *)malloc(sizeof(Student) * MAXSIZE);
    l.maxsize=MAXSIZE;
     for(int i=0;i<MAXSIZE;i++){
        l.data[i].number=0;
        l.data[i].name="a";
    }
    l.length=0;

}